package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.system.domain.RuleReceiver;
import com.ruoyi.system.domain.RuleWarningNoticeType;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WarningRule;
import com.ruoyi.system.service.IWarningRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预警规则Controller
 * 
 * @author ruoyi
 * @date 2021-06-19
 */
@RestController
@RequestMapping("/system/rule")
public class WarningRuleController extends BaseController
{
    @Autowired
    private IWarningRuleService warningRuleService;


    /**
     * 查询预警规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:rule:list')")
    @GetMapping("/list")
    public TableDataInfo list(WarningRule warningRule)
    {
        startPage();
        warningRule.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        warningRule.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        warningRule.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<WarningRule> list = warningRuleService.selectWarningRuleList(warningRule);
        return getDataTable(list);
    }

    /**
     * 导出预警规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:rule:export')")
    @Log(title = "预警规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WarningRule warningRule)
    {
        warningRule.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        warningRule.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        warningRule.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<WarningRule> list = warningRuleService.selectWarningRuleList(warningRule);
        ExcelUtil<WarningRule> util = new ExcelUtil<WarningRule>(WarningRule.class);
        return util.exportExcel(list, "预警规则数据");
    }

    /**
     * 获取预警规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:rule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        WarningRule rule= warningRuleService.selectWarningRuleById(id);
        WarningRule warningRule= warningRuleService.getWarningRuleData(rule);
        return AjaxResult.success(warningRule);
    }

    /**
     * 新增预警规则
     */
    @PreAuthorize("@ss.hasPermi('system:rule:add')")
    @Log(title = "预警规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarningRule warningRule)
    {
        warningRule.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        warningRule.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        warningRule.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        return toAjax(warningRuleService.insertWarningRule(warningRule));
    }

    /**
     * 修改预警规则
     */
    @PreAuthorize("@ss.hasPermi('system:rule:edit')")
    @Log(title = "预警规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarningRule warningRule)
    {
        return toAjax(warningRuleService.updateWarningRule(warningRule));
    }

    /**
     * 删除预警规则
     */
    @PreAuthorize("@ss.hasPermi('system:rule:remove')")
    @Log(title = "预警规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(warningRuleService.deleteWarningRuleByIds(ids));
    }

    /**
     * 规则状态修改
     */
    @PreAuthorize("@ss.hasPermi('monitor:rule:edit')")
    @Log(title = "预警规则", businessType = BusinessType.UPDATE)
    @PutMapping("/changeEnabledState")
    public AjaxResult changeEnabledState(@RequestBody WarningRule warningRule) throws SchedulerException
    {
        WarningRule newRule = warningRuleService.selectWarningRuleById(warningRule.getId());
        newRule.setEnabledState(warningRule.getEnabledState());
        return toAjax(warningRuleService.updateWarningRule(newRule));
    }


}
